package healththink

import grails.test.*
import org.junit.*

class ForumIntegrationTests extends GrailsUnitTestCase{
  
	@Test
    void testForumWithPosts(){
        Facility fac = HealthThink.getStaticFacility()
        fac.facilitySite = HealthThink.getStaticFacilitySite()
        User user = HealthThink.getStaticUser()
		
		user.save(flush:true)
		fac.save(flush:true)
		
		FacilityUserLink ful = FacilityUserLink.link(user, fac)
		ful.userType = UserType.Contributor
		
		ful.save(flush:true)
		
		user.addToFacilities(ful)
		fac.addToFacilityUsers(ful)

        Forum forum = HealthThink.getStaticForum()
        Post post = new Post(message: "New Post")
        forum.addToPosts(post)
		
        user.addToForums(forum)
        fac.addToForums(forum)
		
		user.save(flush:true)
		fac.save(flush:true)
        
		forum.addToPosts(new Post(message: "New Post 1"))
		forum.addToPosts(new Post(message: "New Post 2"))
		forum.addToPosts(new Post(message: "New Post 3"))
		
		assertNotNull (forum.save(flush:true))
		assertNotNull (user.save(flush:true))
        assertNotNull (fac.save(flush:true))
        
		//extract the objects here, make sure cascade worked
        //extract Post, Forum and FacilityUserLink for test case
		
		List targetForum = Forum.findAllByFacility(fac)
		assertNotNull (targetForum) 
		
		//forum.delete()
		//fac.delete()
		//user.delete()
    }
    
}
